package com.stu.stusystem.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.FatalBeanException;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: cxt
 * @time: 2021/3/19
 * 拷贝对象:不复制对象地址
 */
@Slf4j
public class CopyBeanUtil extends BeanUtils {

    /**
     * 拷贝单个对象,不忽略null
     *
     * @param source 源对象
     * @param target 目标对象
     */
    public static void copyNoIgnore(Object source, Object target) {
        if (source == null || target == null) {
            return;
        }
        copyProperties(source, target);
    }

    /**
     * 拷贝单个对象,忽略null，传入类class
     *
     * @param source 源对象
     * @param target 目标对象
     * @return 拷贝后的对象
     */
    public static <T> T copyBean(Object source, Class<T> target) {
        T t = null;
        try {
            t = target.newInstance();
            copyIgnore(source, t);
        } catch (Exception e) {
            log.error("拷贝bean出错:", e);
        }
        return t;
    }

    /**
     * 拷贝多个对象
     *
     * @param re 源数据
     * @param ta 目标数据类型.class
     * @return 拷贝后的list
     */
    public static <T> List<T> copyList(List<?> re, Class<T> ta) {
        ArrayList<T> list = new ArrayList<>(re.size()); // 返回拷贝后的集合
        if (re.size() != 0) {
            try {
                for (Object t : re) {
                    if (t == null) {
                        list.add(null);
                        continue;
                    }
                    T t1 = ta.newInstance();
                    copyIgnore(t, t1);
                    list.add(t1);
                }
            } catch (Exception e) {
                log.error("复制List<Bean>出错：", e);
            }
        }
        return list;
    }

    /**
     * 拷贝单个对象,忽略null
     *
     * @param source 源对象
     * @param target 目标对象
     */
    public static void copyIgnore(Object source, Object target) {
        Assert.notNull(source, "Source must not be null");
        Assert.notNull(target, "Target must not be null");
        Class<?> actualEditable = target.getClass();
        PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
        int var8 = targetPds.length;
        for (int var9 = 0; var9 < var8; ++var9) {
            PropertyDescriptor targetPd = targetPds[var9];
            Method writeMethod = targetPd.getWriteMethod();
            if (writeMethod != null) {
                PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
                if (sourcePd != null) {
                    Method readMethod = sourcePd.getReadMethod();
                    if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
                        try {
                            if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                                readMethod.setAccessible(true);
                            }
                            Object value = readMethod.invoke(source);
                            // 这里判断下值为null时候不拷贝
                            if (value != null){
                                if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                    writeMethod.setAccessible(true);
                                }
                                writeMethod.invoke(target, value);
                            }
                        } catch (Throwable var15) {
                            throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", var15);
                        }
                    }
                }
            }
        }

    }
}
